<template>
  <el-container class="home-container">
    <!-- 头部区 -->
    <el-header>
      <div class="avatar-box">
        <img src="../assets/images/1.png" class="avatar-img"/>
        <span>后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>

    <!-- 侧边栏菜单区 -->
    <el-container>
      <el-aside style="width: 210px;">
        <el-menu background-color="transparent" text-color="#fff" router unique-opened :default-active="activePath" active-text-color="#409eff">
          <!-- 一级菜单区 -->
          <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
            <template slot="title">
              <i :class="item.iconfont"></i>
              <span>{{ item.title }}</span>
            </template>
            <!-- 二级菜单区 -->
            <el-menu-item :index="i.path" v-for="i in item.menuItem" :key="i.id" @click="saveNavState(i.path)">
              <template slot="title">
                <i :class="i.iconfont"></i>
                <span>{{ i.title }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>

      <!-- 内容主体区 -->
      <el-main>
        <!-- 路由跳转 -->
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menuList: [],
      activePath: ''
    }
  },
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    // 退出登录
    async logout() {
      // 弹框询问用户是否确定退出登录
      const confirmResult = await this.$confirm('您是否确定要退出登录?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'info'
      }).catch(err => err)
      if (confirmResult === 'confirm') {
        window.sessionStorage.clear() // 清除 session
        this.$router.push('/login') // 回到登录页面
        return this.$message.success('退出成功')
      }
      this.$message.info('已取消操作')
    },
    async getMenuList() {
      const { data: res } = await this.$axios.get('/menu/getMenuList.do')
      if (!res.flag) return this.$message.error(res.message)
      this.menuList = res.data
    },
    saveNavState(activePath) {
      // 将当前路径存入 session, 防止刷新造成影响
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}

.el-header {
  border: 1px solid #eee;
  // background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
  }
  img {
    height: 60px;
    width: 60px;
    margin-right: 15px;
  }
}

.el-aside {
  border: 1px solid #eee;
  border-top: 0px;
  // background-color: #333744;
  height: calc(100vh - 60px);
  .el-menu {
    border-right: none;
  }
  .editor_box {
    text-align: center;
    line-height: 12px;
    color: rgb(153, 154, 170);
    position: absolute;
    bottom: 10px;
    font-size: 10px;
    margin-bottom: 0px;
    > div {
      padding: 3px;
      margin: 3px;
    }
  }
}

.el-main {
  // background-color: #eaedf1;
  height: calc(100vh - 60px);
}

.iconfont {
  margin-right: 10px;
  color: rgb(134, 131, 131);
  align-content: center;
}
</style>
